﻿using BasicInformation.API.Application.Command.LineCommand;
using BasicInformation.API.Application.Command.OwnerCommand;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace BasicInformation.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OwnerController : ControllerBase
    {
        private readonly IMediator mediator;

        public OwnerController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 货主新增
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> OwnerAdd(OwnerCreateCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 货主删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> OwnerDelete([FromQuery] OwnerDeleteCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 货主批量删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> OwnerBatchDelete([FromQuery] OwnerBatchDeleteCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 货主修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> OwnerUpdate(OwnerUpdateCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        ///上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult FileUpLoad(IFormFile file)
        {
            var FileUrl = Directory.GetCurrentDirectory() + @"\wwwroot\Pic\" + file.FileName;
            using (FileStream fs = new FileStream(FileUrl, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            var json = JsonConvert.SerializeObject(file.FileName);
            return Ok(json);
        }
    }
}
